<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function errorResult($msg="网络繁忙",$code=400,$data=[]){
    $returnData = ["success"=>false,"code"=>$code,"msg"=>$msg];
    if($data){
        $returnData["data"] = $data;
    }
    return json_encode($returnData);
}

function successResult($data=[],$msg="success"){
    if(is_string($data)){
        $msg = $data;
        $data = [];
    }
    $returnData = ["success"=>true,"code"=>200,"msg"=>$msg];
    if($data){
        $returnData["data"] = $data;
    }
    return json_encode($returnData);
}

//截取数字
function roundNumber($num, $accuracy = 2){
    $numArr = explode('.', strval($num));
    if(!isset($numArr[1])){
        return $num;
    }
    
    $numArr[1] = substr($numArr[1], 0, $accuracy);
    
    return floatval($numArr[0] . '.' . $numArr[1]);
    
}


function valiMobile($mobile){
    return preg_match("/^1[3|5|7|8|9]\d{9}$/",$mobile);
}


function redisUserKey($userId) {
    return 'user_' . $userId;
}

function redisUserChatPraiseKey($userId) {
    return 'user_chat_praise_' . $userId;
}

function redisChatKey($chatId) {
    return 'chat_' . $chatId;
}

function redisChatPraisekey($chatId) {
    return 'chat_praise_' . $chatId;
}

